package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.retrofitModule.response.ChangLongResponse;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import ui.IToast;
import utils.StringUtils;

/**
 * 作者：lm
 * <p>
 * 创建日期：2018/8/24$
 * <p>
 * 描述：俩面长龙适配器
 */
public class ChangLongAdapter extends RecyclerView.Adapter<ChangLongAdapter.ChangLongViewHolder> {
    private Context context;
    private ArrayList<ChangLongResponse> changLongList;
    private ChangLongListener changLongListener;
    private int inPosion;
private   String select="";
    public ChangLongAdapter(Context context, ArrayList<ChangLongResponse> changLongList) {
        this.context = context;
        this.changLongList = changLongList;
    }
    public void setChangLongListener(ChangLongListener changLongListener){
        this.changLongListener=changLongListener;
    }

    @Override
    public ChangLongViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ChangLongViewHolder(LayoutInflater.from(context).inflate(R.layout.item_fragment_chang_long, null));
    }

    @Override
    public void onBindViewHolder(final ChangLongViewHolder holder, final int position) {
        if (changLongList.get(position) != null) {
            holder.llLotteryBuy.setVisibility(View.GONE);
            holder.tvLotterySingle.setTextColor(context.getResources().getColor(R.color.color5));
            holder.tvLotterySingle.setBackground(context.getResources().getDrawable(R.drawable.bg_select_no));
            holder.tvLotteryDouble.setTextColor(context.getResources().getColor(R.color.red_one));
            holder.tvLotteryDouble.setBackground(context.getResources().getDrawable(R.drawable.bg_select_no));
            holder.tvLotteryMoney.setImeOptions(EditorInfo.IME_ACTION_DONE);
            final ChangLongResponse bean = changLongList.get(position);
            holder.tvLotteryName.setText(bean.getShow_name());
            holder.tvLotteryWhich.setText("" + bean.getLx_count());
            holder.tvLotterySelectName.setText(bean.getLx_name());
            final ChangLongResponse.OddsBean bean1 = bean.getOdds().get(0);
            final ChangLongResponse.OddsBean bean2 = bean.getOdds().get(1);
            holder.tvLotterySingle.setText(bean1.getName());
            holder.tvLotteryDouble.setText(bean2.getName());
            holder.tvLotterySingle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    select =bean1.getName();
                    changLongListener.ChangLongClick(position, inPosion, "",select);
                    holder.tvLotterySingle.setTextColor(context.getResources().getColor(R.color.tab_selected));
                    holder.tvLotterySingle.setBackground(context.getResources().getDrawable(R.drawable.bg_select_yes));
                    holder.tvLotteryDouble.setTextColor(context.getResources().getColor(R.color.red_one));
                    holder.tvLotteryDouble.setBackground(context.getResources().getDrawable(R.drawable.bg_select_no));
                    holder.tvLotteryOddsName.setText(bean1.getName());
                    holder.tvLotteryOdds.setText(bean1.getMax_odds().get_$0());
                    holder.llLotteryBuy.setVisibility(View.VISIBLE);
                    inPosion =0;
                }
            });
            holder.tvLotteryDouble.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    select =bean2.getName();
                    changLongListener.ChangLongClick(position, inPosion, "",select);
                    holder.tvLotterySingle.setTextColor(context.getResources().getColor(R.color.color5));
                    holder.tvLotterySingle.setBackground(context.getResources().getDrawable(R.drawable.bg_select_no));
                    holder.tvLotteryDouble.setTextColor(context.getResources().getColor(R.color.tab_selected));
                    holder.tvLotteryDouble.setBackground(context.getResources().getDrawable(R.drawable.bg_select_yes));
                    holder.tvLotteryOddsName.setText(bean2.getName());
                    holder.tvLotteryOdds.setText(bean2.getMax_odds().get_$0());
                    holder.llLotteryBuy.setVisibility(View.VISIBLE);
                    inPosion=1;
                }
            });
            holder.tvLotteryQuit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    holder.llLotteryBuy.setVisibility(View.GONE);
                    holder.tvLotterySingle.setTextColor(context.getResources().getColor(R.color.color5));
                    holder.tvLotterySingle.setBackground(context.getResources().getDrawable(R.drawable.bg_select_no));
                    holder.tvLotteryDouble.setTextColor(context.getResources().getColor(R.color.red_one));
                    holder.tvLotteryDouble.setBackground(context.getResources().getDrawable(R.drawable.bg_select_no));
                }
            });
            holder.tvLotterySure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String money = holder.tvLotteryMoney.getText().toString().trim();

                    if (StringUtils.isEmpty(money)) {
                        IToast.show("请检查输入金额", context);
                    } else {
                        changLongListener.ChangLongClick(position, inPosion, money,select);
                    }
                }
            });

        }
    }

    @Override
    public int getItemCount() {
        return changLongList.size();
    }

    public class ChangLongViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_lottery_name)
        TextView tvLotteryName;
        @BindView(R.id.tv_lottery_which)
        TextView tvLotteryWhich;
        @BindView(R.id.tv_lottery_single)
        TextView tvLotterySingle;
        @BindView(R.id.tv_lottery_double)
        TextView tvLotteryDouble;
        @BindView(R.id.tv_lottery_select_name)
        TextView tvLotterySelectName;
        @BindView(R.id.tv_lottery_odds_name)
        TextView tvLotteryOddsName;
        @BindView(R.id.tv_lottery_odds)
        TextView tvLotteryOdds;
        @BindView(R.id.tv_lottery_money)
        EditText tvLotteryMoney;
        @BindView(R.id.tv_lottery_quit)
        TextView tvLotteryQuit;
        @BindView(R.id.tv_lottery_sure)
        TextView tvLotterySure;
        @BindView(R.id.ll_lottery_buy)
        LinearLayout llLotteryBuy;

        public ChangLongViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public interface ChangLongListener{
        void ChangLongClick(int posion,int v,String money,String select);
    }
}
